import { getAddress, getlatlngAddress } from "../../utils/address"
Component({
    data: {
        //标记点
        makersData: [
            {
                id: 1,
                longitude: 119.62,  //经度
                latitude: 29.12, //纬度
                iconPath: "../../image/icon/address-zs.png",
                width: 30,
                height: 38,
            }
        ],
    },
    properties: {
        //配置项
        config: {
            type: Object,
            value: {
                scale: 16, //缩放级别，取值范围为3-20
                minScale: 3, //最小缩放级别
                maxScale: 20, //最大缩放级别
                enable3D: true, //展示3D楼块
                enableZoom: true, //是否支持缩放
                enableScroll: true, //是否支持拖动
                enableBuilding: true, //是否展示建筑物
                tapFlag: true, //是否支持点击地图打开第三方地图功能
            }
        },
        //地图中心点
        latLng: {
            type: String,
            value: "119.62,29.12",
            observer: function (val) {
                if (val) {
                    let arr = val.split(",").map(Number),
                        longitude = "makersData[0].longitude", latitude = "makersData[0].latitude";
                    this.setData({
                        [longitude]: arr[0],
                        [latitude]: arr[1],
                    });
                };
            },
        },
        // makers: {
        //     type: Array,
        //     value: [],
        //     observer: function (val) {
        //         if (val.length != 0) {
        //             val.forEach(item => {
        //                 item.iconPath = item.iconPath.slice(3, item.iconPath.length);
        //             });
        //             this.setData({ makersData: val });
        //         };
        //     }
        // },
    },
    attached() {
    },
    detached() { },
    methods: {
        //点击地图事件
        mapClick(e) {
            let _this = this.properties;
            if (_this.config.tapFlag) {
                wx.openLocation({
                    latitude: this.data.makersData[0].latitude,
                    longitude: this.data.makersData[0].longitude,
                });
            } else {
                //标记makers标记点
                this.setData({
                    "makersData[0].longitude": e.detail.longitude,
                    "makersData[0].latitude": e.detail.latitude,
                });
                //地址逆解析(经纬度转具体地址)
                getlatlngAddress({ latitude: e.detail.latitude, longitude: e.detail.longitude }).then(res => {
                    let obj = {
                        longitude: e.detail.longitude,
                        latitude: e.detail.latitude,
                        address: res.recommend,
                    };
                    this.triggerEvent("mapTap", obj);
                });
            };

        },
    }
})